package com.fingard.dsp.bank.directbank.gdrc01;

import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.directbank.gdrc01.util.Tool;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/10.
 * 单笔支付、单笔合批支付
 */
public class GDRC01SinglePay extends GDRC01Base implements ISinglePay {

    private static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,STATE,RESERVE1) values (?,?,?,?,?,?);";
    private static final String SELECTSEQ = "SELECT STATE,RESERVE1 FROM REQSEQIDMAPPING WHERE REQSEQID=?;";

    /**
     * 单笔转账
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception{
        return d1908SglPay_A(p_req1908);
    }

    /**
     * 集团包含成员单位
     * @param p_req1908
     * @return
     * @throws Exception
     */
    private RetAtsSingle d1908SglPay_A(ReqSingle p_req1908) throws Exception{
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        Connection conn= FGBiz.getLocalDbConn();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GDRC01Header gdrc01Header=new GDRC01Header();
            gdrc01Header.TrsCode="B2EGroupMemEntAccount";//交易码
            gdrc01Header.ERPJnlNo=p_req1908.singleDetail.askNumber;//财务系统交易流水号
            writeCommonHeader(xmlTextWriter,gdrc01Header);
            xmlTextWriter.writeEndElement();
            XElement body = new XElement("Body","");
            //获取客户编号
            body.addChild(new XElement("CustNo",getCustNo(p_req1908).trim()));//客户编号
            body.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));//付款方银行账号
            body.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));//付款方银行账名
            body.addChild(new XElement("PayerAcDept",""));//付款方开户行代码
            body.addChild(new XElement("PayerAcDeptName",""));//付款方开户行名称
            body.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));//收款人单位账户
            body.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));//收款人单位名称
            body.addChild(new XElement("UnionDeptId",p_req1908.singleDetail.cnapsCode));//收款人开户机构代码
            body.addChild(new XElement("UnionBankName",p_req1908.singleDetail.cnapsName));//收款人开户机构名称
            if("1".equals(p_req1908.singleDetail.sameBnk)){//同行
                body.addChild(new XElement("PayeeBankType","0"));//收款方是否是本行账户
            }else {
                body.addChild(new XElement("PayeeBankType","1"));//收款方是否是本行账户
            }
            body.addChild(new XElement("Priority",p_req1908.singleDetail.paySpd));//加急标志
            body.addChild(new XElement("PayeeCurrency",convertCurToBank(p_req1908.singleDetail.payCur)));//币种
            body.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));//发生金额
            body.addChild(new XElement("chineseAmountId1", Tool.change(Double.parseDouble(p_req1908.singleDetail.payAmount))));//大写
            body.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));//摘要
            body.addChild(new XElement("Operator",bankFront.operator));//操作人
            body.addChild(new XElement("OperTime", Format.DateTime14Format.format(new Date())));//操作时间
            body.addChild(new XElement("Status","1"));//状态
            //nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();

            //获取客户号后间隔5发送支付请求
            if(getBankConfig().getValueAsBool("isSleep")){
                Thread.sleep(5000);
            }
            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

            //测试
            //bankResult[0]="";
            //bankResult[1]="<Message>   <Head>      <IBSReturnCode>000000</IBSReturnCode> <IBSReturnMsg>成功</IBSReturnMsg>   </Head>   <Body>      <RecFeeAmt>0.00</RecFeeAmt>      <PayFeeAmt>0.00</PayFeeAmt>   </Body></Message>\n";
            //WriteBankLogStep3(bankResult[1]);

            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].trim(),tmpSbMsg);
                if(tmpBkReader!=null) {
                    String respCode=tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//响应码
                    String respInfo=tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//响应信息
                    if(!"".equals(respCode) && respCode!=null){
                        if("000000".equals(respCode)){
                            //记录此笔订单的状态，供1944状态查询
                            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                            JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, bankFront.bankCode, Format.Date8Format.format(new Date()), p_req1908.singleDetail.askNumber, "000000",respInfo});
                        }else {
                            //记录此笔订单的状态，供1944状态查询
                            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                            JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, bankFront.bankCode, Format.Date8Format.format(new Date()), p_req1908.singleDetail.askNumber, "111111",respInfo});
                        }
                        detail.respCode = "0";
                        detail.respInfo = "指令提交成功，交易处理中";
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;
                        detail.payInfoCode = "000000";//处理中
                        detail.transState = detail.payInfoCode;
                        detail.transAmt = "";//交易金额
                        detail.outTransID = "";
                    }else {
                        detail.respCode = "0";
                        detail.respInfo = "响应状态不明";
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;
                        detail.transState = "PAYING";
                        detail.payInfoCode = detail.transState;
                    }
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else {
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("同行或者跨行转账出现异常:"+e);
        }finally {
            conn.close();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    /**
     * 单笔转账状态查看
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        return d1944QrySglPay_A(p_req1944);
    }

    /**
     * 集团含单位成员
     *
     * 广东农信银行：集团接口付款未提供付款状态查询接口，直接通过付款响应结果判断付款状态
     * 成功：000000
     * 失败：!000000
     * 无法确定：未返回，无记录
     *
     * @param p_req1944
     * @return
     * @throws Exception
     */
    private RetAtsSingle d1944QrySglPay_A(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        Connection conn= FGBiz.getLocalDbConn();
        try{
            Map<String, Object> selMap=JDBCHelper.execQuerySingle(conn,SELECTSEQ,new Object[]{p_req1944.askNumber});
            if(selMap.size()>0){
                if("000000".equals(selMap.get("STATE"))){
                    detail.respCode = "0";//响应码
                    detail.respInfo = StringHelper.hasAnyChar(selMap.get("RESERVE1")+"")?selMap.get("RESERVE1")+"":"交易成功";//响应结果
                    detail.reqSeqID = p_req1944.askNumber;//交易流水号
                    detail.payInfoCode = "000000";//交易结果无法确定
                    detail.transState = detail.payInfoCode;
                }else if("111111".equals(selMap.get("STATE"))){
                    detail.respCode = "0";//响应码
                    detail.respInfo = StringHelper.hasAnyChar(selMap.get("RESERVE1")+"")?selMap.get("RESERVE1")+"":"交易失败";//响应结果
                    detail.reqSeqID = p_req1944.askNumber;//交易流水号
                    detail.payInfoCode = "111111";//交易结果无法确定
                    detail.transState = detail.payInfoCode;
                }else {
                    detail.respCode = "0";//响应码
                    detail.respInfo = "交易状态不明，请联系银行人员"+p_req1944.askNumber;//响应结果
                    detail.reqSeqID = p_req1944.askNumber;//交易流水号
                    detail.payInfoCode = "222222";//交易结果无法确定
                    detail.transState = detail.payInfoCode;
                }
            }else {
                detail.respCode = "0";//响应码
                detail.respInfo = "交易状态不明，请联系银行人员"+p_req1944.askNumber;//响应结果
                detail.reqSeqID = p_req1944.askNumber;//交易流水号
                detail.payInfoCode = "222222";//交易结果无法确定
                detail.transState = detail.payInfoCode;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询同行转账状态时异常："+e);
        }finally {
            conn.close();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    private RetAtsSingle d1944QrySglPay_C(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        retAtsSingle.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo ="";
        try{
            int tmpNowReadStart = 1;//起始笔数
            int tmpNowReadNum = 0;//已查询笔数
            Map<String,Integer> tmpTotalNum = new HashMap<String, Integer>();
            while (tmpNowReadNum==0 || tmpNowReadNum<tmpTotalNum.get("recordCount")) {
                //拼接请求报文头
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                GDRC01Header gdrc01Header = new GDRC01Header();
                gdrc01Header.TrsCode = "B2EGroupActResultQry";
                gdrc01Header.ERPJnlNo = p_req1944.askNumber;//财务系统交易流水号
                writeCommonHeader(xmlTextWriter, gdrc01Header);
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeElementString("AcNo", p_req1944.payAct);//账号
                xmlTextWriter.writeElementString("JnlNo", p_req1944.askNumber);//财务流水号
                xmlTextWriter.writeElementString("BeginNo", tmpNowReadStart + "");//账号BeginNo
                xmlTextWriter.writeElementString("RecordNum", getBankConfig().getValueAsString("pageSize"));//财务流水号
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].trim(), tmpSbMsg);
                    if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//响应码
                        String respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//响应信息
                        if ("000000".equals(respCode)) {
                            String recordCount = tmpBkReader.getFirstText("/Message/Body/TotalCount");//获取总记录数
                            String totalCount = tmpBkReader.getFirstText("/Message/Body/RecordCount");//获取页面记录条数
                            if (tmpTotalNum.containsKey("recordCount")) {
                                if (tmpTotalNum.get("recordCount").intValue() != Integer.valueOf(recordCount).intValue()) {
                                    tmpRespCode = "-1";
                                    tmpRespInfo = "相同条件前后查出来的总数不一致";
                                    break;
                                }
                            } else {
                                if ("0".equals(recordCount)) {
                                    tmpRespCode = "0";
                                    tmpRespInfo = "未查询到结果信息";
                                    break;
                                }
                                tmpTotalNum.put("recordCount", Integer.valueOf(recordCount));//记录总条数
                            }
                            int readCount = 0;
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                XElement readXElement = tmpBkReader.readedXElement;
                                RetAtsSglDetail detail = new RetAtsSglDetail();
                                detail.respCode = "0";//响应码
                                detail.respInfo = readXElement.getSingleTextNotNull("TransResult");//响应结果
                                detail.reqSeqID = readXElement.getSingleTextNotNull("CoreJnlno");//交易流水号
                                String tranState = readXElement.getSingleTextNotNull("Status");//交易流水号
                                if(tranState.equals("0")){
                                    detail.payInfoCode = "000000";//成功
                                }else{
                                    detail.payInfoCode = "111111";//失败
                                }
                                detail.transState = detail.payInfoCode;
                                retAtsSingle.writeRetDetail(detail);
                                readCount++;
                            }
                            //查询记录数和遍历数不一致
                            if(readCount!=Integer.valueOf(totalCount)){
                                tmpRespCode = "-1";
                                tmpRespInfo = "统计明细有误";
                                break;
                            }
                            tmpNowReadStart +=readCount;
                            tmpNowReadNum +=readCount;
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = respInfo;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = bankResult[1];
                        break;
                    }
                }
            }
        }catch (Exception e){
            retAtsSingle.writeRespCode("-1");
            retAtsSingle.writeRespInfo("发生异常");
            e.printStackTrace();
            WriteBankLogLn("出现异常:"+e);
        }
        retAtsSingle.writeRespCode(tmpRespCode);
        retAtsSingle.writeRespInfo(tmpRespInfo);
        retAtsSingle.writeEnd();
        return retAtsSingle;
    }


    private RetAtsSingle d1908SglPay_B(ReqSingle p_req1908) throws Exception{
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GDRC01Header gdrc01Header=new GDRC01Header();
            if("1".equals(p_req1908.singleDetail.sameBnk)){//同行转账
                gdrc01Header.TrsCode="B2EBankInnerTransfer";//交易码
            }else {//跨行转账
                gdrc01Header.TrsCode="B2ECrossBankTransfer";//交易码
            }
            gdrc01Header.ERPJnlNo=p_req1908.singleDetail.askNumber;//财务系统交易流水号
            writeCommonHeader(xmlTextWriter,gdrc01Header);
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeElementString("SubmitNo",p_req1908.singleDetail.askNumber);//凭证号
            xmlTextWriter.writeStartElement("List");
            xmlTextWriter.writeStartElement("Map");
            xmlTextWriter.writeElementString("ListNo",p_req1908.singleDetail.askNumber);//序号
            xmlTextWriter.writeElementString("PayerAcNo",p_req1908.singleDetail.payAct);//转出账号
            xmlTextWriter.writeElementString("PayeeAcNo",p_req1908.singleDetail.recAct);//转入账号
            xmlTextWriter.writeElementString("PayeeAcName",p_req1908.singleDetail.recName);//转入帐号户名
            xmlTextWriter.writeElementString("Amount",p_req1908.singleDetail.payAmount);//金额
            if("0".equals(p_req1908.singleDetail.sameBnk)){//跨行转账
                xmlTextWriter.writeElementString("UnionDeptId",p_req1908.singleDetail.cnapsCode);//收款行（网点）行号
                xmlTextWriter.writeElementString("PayeeBankName",p_req1908.singleDetail.cnapsName);//收款行名称
            }
            xmlTextWriter.writeElementString("Remark",p_req1908.singleDetail.purpose);//摘要
            if("0".equals(p_req1908.singleDetail.sameBnk)){//跨行转账
                xmlTextWriter.writeElementString("AttachInfo",p_req1908.singleDetail.memo);//转账附言
            }
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndDocument();
            //拼接请求报文体
            /*XmlTextWriter xmlTextWriter2 = new XmlTextWriter();
            XElement body = new XElement("Body","");
            body.addChild(new XElement("ListNo",p_req1908.singleDetail.askNumber));//序号
            body.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));//转出账号
            body.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));//转入账号
            body.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));//转入帐号户名
            body.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));//金额
            if("0".equals(p_req1908.singleDetail.sameBnk)){//跨行转账
                body.addChild(new XElement("UnionDeptId",p_req1908.singleDetail.cnapsCode));//收款行（网点）行号
                body.addChild(new XElement("PayeeBankName",p_req1908.singleDetail.cnapsName));//收款行名称
            }
            body.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));//摘要
            if("0".equals(p_req1908.singleDetail.sameBnk)){//跨行转账
                body.addChild(new XElement("AttachInfo",p_req1908.singleDetail.memo));//转账附言
            }
            *//*nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());*//*
            xmlTextWriter2.writeXElement(body);
            xmlTextWriter2.writeEndDocument();
            //将原报文内容加入循环体中
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeElementString("SubmitNo",p_req1908.singleDetail.askNumber);//凭证号
            xmlTextWriter.writeStartElement("List");
            xmlTextWriter.writeStartElement("Map");
            xmlTextWriter.directWrite(xmlTextWriter2.toXmlString().substring(6,xmlTextWriter2.toXmlString().length()-7));
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndDocument();*/
            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

            //测试
            //bankResult[0]="";
            //bankResult[1]="<Message>  <Head>    <IBSReturnCode>000000</IBSReturnCode>    <IBSReturnMsg>成功</IBSReturnMsg>    <IBSJnlNo>111111111</IBSJnlNo>  </Head>  <Body>    <List>      <Map>        <ListNo>11</ListNo>        <ReturnCode>000000</ReturnCode>        <ReturnMsg>成功</ReturnMsg>        <Fee>0</Fee>      </Map>    </List>  </Body></Message>\n";
            //WriteBankLogStep3(bankResult[1]);

            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].trim(),tmpSbMsg);
                if(tmpBkReader!=null) {
                    String respCode=tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//响应码
                    String respInfo=tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//响应信息
                    if("000000".equals(respCode)){
                        String returnCode=tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnCode");//响应码
                        String returnMsg=tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnMsg");//响应消息
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = tmpBkReader.getFirstText("/Message/Head/IBSJnlNo");
                        detail.payInfoCode = returnCode.equals("000000")?returnCode:"111111";//将响应码为非000000的统一改为111111
                        detail.transState = detail.payInfoCode;
                        detail.transAmt = "";//交易金额
                        detail.outTransID = tmpBkReader.getFirstText("/Message/Head/IBSJnlNo");
                    }else {
                        detail.respCode ="-1";
                        detail.respInfo = respInfo;
                    }
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else {
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("同行或者跨行转账出现异常:"+e);
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    /**
     * 集团不含单位成员
     * @param p_req1944
     * @return
     * @throws Exception
     */
    private RetAtsSingle d1944QrySglPay_B(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GDRC01Header gdrc01Header=new GDRC01Header();
            if("1".equals(p_req1944.sameBnk)){//同行
                gdrc01Header.TrsCode="B2ETrsResQryByERPJnlNo";//交易码
            }else {//跨行
                gdrc01Header.TrsCode="B2ENewCrossTransferResultQryByERPJnlNo";//交易码
            }
            gdrc01Header.ERPJnlNo=p_req1944.reqHeader.transSeqID ;//财务系统交易流水号
            writeCommonHeader(xmlTextWriter,gdrc01Header);
            xmlTextWriter.writeEndElement();
            //拼接请求报文体
            XElement body = new XElement("Body","");
            if(p_req1944.askNumber.isEmpty()){
                detail.respCode ="-1";
                detail.respInfo = "财务系统交易流水号不能为空";
            }else {
                body.addChild(new XElement("JnlNo", p_req1944.askNumber));//财务系统交易流水号
                xmlTextWriter.writeXElement(body);
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

                //测试
                //bankResult[0]="";
                //同行
                //bankResult[1]="<?xml version=\"1.0\" encoding=\"utf-8\"?><Message>  <Head>    <IBSReturnCode>000000</IBSReturnCode>    <IBSReturnMsg>成功</IBSReturnMsg>    <IBSJnlNo>111111111</IBSJnlNo>  </Head>  <Body>    <TotalCount>1</TotalCount>    <List>      <Map>        <IBSJnlNo>11111</IBSJnlNo>        <ERPJnlNo>1111111</ERPJnlNo>        <ListNo>111111</ListNo>        <TransTime>20180808141414</TransTime>        <ERPTrsTimestamp>20180808141414</ERPTrsTimestamp>        <TransCode>0</TransCode>        <PayerAcNo>00121211211121</PayerAcNo>        <Currency>CNY</Currency>        <PayeeAcNo>12121321131</PayeeAcNo>        <PayeeAcName>哈哈</PayeeAcName>        <BankCode>1021</BankCode>        <BankName>随便银行</BankName>        <Amount>10</Amount>        <Fee>0</Fee>        <Remark>11111111</Remark>        <TransResult>0</TransResult>      </Map>    </List>  </Body></Message>\n";
                //跨行
                //bankResult[1]="<?xml version=\"1.0\" encoding=\"utf-8\"?><Message>  <Head>    <IBSReturnCode>000000</IBSReturnCode>    <IBSReturnMsg>成功</IBSReturnMsg>    <IBSJnlNo>111111111</IBSJnlNo>  </Head>  <Body>    <TotalCount>1</TotalCount>    <List>      <Map>        <IBSJnlNo>11111</IBSJnlNo>        <ERPJnlNo>1111111</ERPJnlNo>        <ListNo>111111</ListNo>        <TransTime>20180808141414</TransTime>        <ERPTrsTimestamp>20180808141414</ERPTrsTimestamp>        <TransCode>0</TransCode>        <PayerAcNo>00121211211121</PayerAcNo>        <Currency>CNY</Currency>        <PayeeAcNo>12121321131</PayeeAcNo>        <PayeeAcName>哈哈</PayeeAcName>        <BankCode>1021</BankCode>        <BankName>随便银行</BankName>        <Amount>10</Amount>        <Fee>0</Fee>        <Summary>敷衍</Summary>        <AcctDiscri>交易描述</AcctDiscri>        <Stat>06</Stat>      </Map>    </List>  </Body></Message>\n";
                //WriteBankLogStep3(bankResult[1]);

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (xmlSbReader != null) {
                        String rsp_no = xmlSbReader.getFirstText("/Message/Head/IBSReturnCode");//响应码
                        String rsp_msg = xmlSbReader.getFirstText("/Message/Head/IBSReturnMsg");//响应信息
                        if("000000".equals(rsp_no)){
                            if(xmlSbReader.read("/Message/Body/List/Map")){
                                detail.respCode = "0";//响应码
                                detail.respInfo = rsp_msg;//响应结果
                                detail.reqSeqID = xmlSbReader.readedXElement.getSingleText("ERPJnlNo").trim();//交易流水号
                                if("1".equals(p_req1944.sameBnk)) {//同行
                                    detail.payInfoCode = xmlSbReader.readedXElement.getSingleText("TransResult").trim();//交易结果
                                }else { //跨行
                                    detail.payInfoCode = xmlSbReader.readedXElement.getSingleText("Stat").trim();//交易结果
                                }
                                detail.transState = detail.payInfoCode;
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = rsp_msg;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询同行转账状态时异常："+e);
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception{
        return  null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception{
        return  null;
    }
}
